﻿using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

public class GuiCamera : MonoBehaviour
{
	public GUISkin guiSkin;
	public GUISkin guiAlt;
	public GUISkin guiDef;
	public GUISkin guiUs;
	public GUISkin guiEn;
	public bool techButton;
	public bool isTop = false;
	public bool inTurn = false;
	public bool toStar = false;
	public bool toConfirm = false;
	public bool toColony = false;

	public string strCommand;

	private bool optButtons = false;

	public List<string> stech = new List<string>();					// to store the 6 technology selections in the 6 technology categories
	public List<Kingdom.TechByCost> nTech = new List<Kingdom.TechByCost>();					// to store the maximum number of technologies allowed for parallel development = 3

	private Rect rectWin = new Rect(10, 10, 360, 540);
	private Rect rectList = new Rect(340, 10, 300, 460);											// owned technolgies
	private Rect rectTop = new Rect(Screen.width / 2 - 110, 60, 220, 60);
	private Rect rectSummary = new Rect(Screen.width - 250, 10, 220, 250);
	private Rect rectInfo = new Rect(10, 10, 300, 460);												// main info window
	private Rect rectFaci = new Rect(340, 10, 300, 460);											// owned facilities
	private Rect rectShip = new Rect(340, 36, 300, 460);

	private Rect rectWin2 = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 30, 200, 60);
	private Rect rectWin3 = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 30, 200, 60);
	private Rect rectDocking = new Rect(Screen.width - 320, Screen.height - 220, 300, 60);
	private Rect rectLaunching = new Rect(Screen.width - 320, Screen.height - 120, 300, 60);
	private Rect rectOptions = new Rect(Screen.width / 2 - 50, Screen.height - 200, 100, 60);

	public PlayerKingdom kd;

	private Vector2 winscrollHead = Vector2.zero;

	private Vector2 winscrollPos = Vector2.zero;
	private Vector2 winscrollPos1 = Vector2.zero;
	private Vector2 winscrollPos2 = Vector2.zero;
	private Vector2 winscrollPos3 = Vector2.zero;
	private Vector2 winscrollPos4 = Vector2.zero;
	private Vector2 winscrollPos5 = Vector2.zero;
	private Vector2 winscrollPos6 = Vector2.zero;

	private Vector2 winscrollPos7 = Vector2.zero;
	private Vector2 winscrollPos8 = Vector2.zero;
	private Vector2 winscrollPos9 = Vector2.zero;

	private bool[] infoKey = new bool[4] { false, false, false, false };


	void Awake()
	{

	}


	// Use this for initialization
	void Start()
	{
		// initialize tech selection arraylist, for 6 technology categories
		for (int i = 0; i < 6; i++)
			stech.Add("");
	}

	// Update is called once per frame
	void Update()
	{

	}

	void OnGUI()
	{
		if (null == kd || Core.shutMain)
			return;

		GUI.skin = guiSkin;

		if (toStar || infoKey[3])
			rectShip = GUILayout.Window(24, rectShip, WinShipList, "Kingdom Fleets");

		if (techButton)
			rectWin = GUILayout.Window(0, rectWin, WinTechContent, "Technology List");

		if (toConfirm)
			rectWin2 = GUILayout.Window(2, rectWin2, WinConfirmFleet, "Confirm Send Fleets");

		if (toColony)
			rectWin3 = GUILayout.Window(5, rectWin3, WinToColony, "Settle Colony Ship");

		if (infoKey[1])
			rectList = GUILayout.Window(1, rectList, WinTechList, "Owned Technology");

		if (isTop && !toStar)
			rectTop = GUILayout.Window(20, rectTop, WinTopShow, "Must Do");

		if (kd.pOwnedTechString.Length > 0 || kd.pUnlockedTechString.Length > 0 || techButton)
			rectSummary = GUILayout.Window(21, rectSummary, WinShowSummary, "Summary");

		if (infoKey[0])
			rectInfo = GUILayout.Window(22, rectInfo, WinInfolList, "Infomation");

		if (infoKey[2])
			rectFaci = GUILayout.Window(23, rectFaci, WinFacilityList, "Owned Facility");

		if (Core.solarOnShow != null && Core.solarOnShow.Docking.Count > 0)
			rectDocking = GUILayout.Window(14, rectDocking, WinDockingShips, "Docking Ships");

		if (Core.solarOnShow.Launching.Count > 0)
			rectLaunching = GUILayout.Window(15, rectLaunching, WinLaunchingShips, "Launching Ships");

		// the rest of the screen buttons
		GUI.skin = guiDef;

		// The next line makes sure it rescales nicely to other resolutions.
		// GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));

		if (optButtons && !isTop && !toStar)
			rectOptions = GUILayout.Window(16, rectOptions, WinOptionList, "Options");

		InitCamera camScript = Camera.main.GetComponent<InitCamera>();

		if (null == camScript)
		{
			Debug.Log("No camera script found!");
			return;
		}

		if (Core.solarOnShow != null && !toConfirm)
		{
			GUI.BeginGroup(new Rect(Screen.width / 2 - 100, 0, 200, 30));
			winscrollHead = GUILayout.BeginScrollView(winscrollHead, GUILayout.Width(160), GUILayout.Height(30));

			if (GUILayout.Button(Core.solarOnShow.GetOwnerText(), Core.solarOnShow.OwnedBy.Count > 0 ? guiAlt.button : guiDef.button))
			{
				DetectSendFleet();
			}

			GUILayout.EndScrollView();
			GUI.EndGroup();
		}

		if (!(isTop || toStar || toConfirm || toColony) && Core.playerLevel > Core.AccessLevel.Player)
		{
			GUI.BeginGroup(new Rect(Screen.width / 2 - 200, Screen.height - 45, 560, 22));
			if (GUI.Button(new Rect(0, 0, 80, 22), "Previous"))
			{
				camScript.follow = false;
				camScript.PreviousNextStar(-1);
			}
			else if (GUI.Button(new Rect(80, 0, 80, 22), "Next"))
			{
				camScript.follow = false;
				camScript.PreviousNextStar(1);
			}
			else if (GUI.Button(new Rect(160, 0, 80, 22), "Home"))
			{
				camScript.follow = false;
				camScript.ScrollHomes();
			}
			else if (GUI.Button(new Rect(240, 0, 80, 22), "Scan Down"))
			{
				camScript.follow = false;
				camScript.ScrollKingdomSolars(-1);			// backward, all reachable solars of current kingdom
			}
			else if (GUI.Button(new Rect(320, 0, 80, 22), "Scan Up"))
			{
				camScript.follow = false;
				camScript.ScrollKingdomSolars(1);				// forward, all reachable solars of current kingdom
			}
			else if (GUI.Button(new Rect(400, 0, 80, 22), "Run"))
			{
				if (null != camScript.starsystem)
					VisualMisc.RunStopStarSystem(camScript.starsystem, true);
			}
			else if (GUI.Button(new Rect(480, 0, 80, 22), "Stop"))
			{
				if (null != camScript.starsystem)
					VisualMisc.RunStopStarSystem(camScript.starsystem, false);
			}
			GUI.EndGroup();
		}

		if (!(isTop || toStar || toConfirm || toColony))
		{
			GUI.BeginGroup(new Rect(Screen.width / 2 - 280, Screen.height - 22, 640, 22));

			if (GUI.Button(new Rect(0, 0, 80, 22), "Home"))
			{
				camScript.follow = false;
				camScript.ReturnHome();
			}

			if (GUI.Button(new Rect(80, 0, 80, 22), "Previous"))
			{
				camScript.follow = false;
				camScript.ScrollSolars(-1, 1);			// backward, conquered only
			}
			else if (GUI.Button(new Rect(160, 0, 80, 22), "Next"))
			{
				camScript.follow = false;
				camScript.ScrollSolars(1, 1);				// forward, conquered only
			}
			else if (GUI.Button(new Rect(240, 0, 80, 22), "Option", guiAlt.button))
			{
				optButtons = !optButtons;
			}
			else if (GUI.Button(new Rect(320, 0, 80, 22), "Scan Down"))
			{
				camScript.follow = false;
				camScript.ScrollSolars(-1, 2);				// backward, non-conquered
			}
			else if (GUI.Button(new Rect(400, 0, 80, 22), "Scan Up"))
			{
				camScript.follow = false;
				camScript.ScrollSolars(1, 2);					// forward, non-conquered
			}
			else if (GUI.Button(new Rect(480, 0, 80, 22), "Technology"))
			{
				techButton = !techButton;

				for (int i = 0; i < infoKey.Length; i++)
					infoKey[i] = false;
			}
			else if (GUI.Button(new Rect(560, 0, 80, 22), "Info"))
			{
				bool temp = !infoKey[0];
				techButton = false;

				// turn off other lists
				for (int i = 0; i < infoKey.Length; i++)
					infoKey[i] = false;

				infoKey[0] = temp;
			}

			GUI.EndGroup();
		}

		if (!(isTop || toStar || toConfirm || toColony))
		{
			if (GUI.Button(new Rect(Screen.width - 100, Screen.height - 75, 100, 25), "Turn") && !inTurn)
			{
				techButton = false;

				for (int j = 0; j < infoKey.Length; j++)
					infoKey[j] = false;

				Core.mainGalaxy.OnTurn();
				nTech = new List<Kingdom.TechByCost>();

				for (int i = 0; i < stech.Count; i++)
					stech[i] = "";
			}

			if (GUI.Button(new Rect(0, Screen.height - 25, 100, 25), "Quit"))
				Application.Quit();
		}

	}

	// windows
	void WinOptionList(int windowID)
	{
		winscrollPos9 = GUILayout.BeginScrollView(winscrollPos9, GUILayout.Width(100), GUILayout.Height(106));

		if (GUILayout.Button("New Game", guiDef.button, GUILayout.Width(91), GUILayout.Height(22)))
		{
			foreach (Building bd in Core.Buildings.Values)
				Debug.Log("ID#: " + bd.FacilityID.ToString() + ", Type: " + bd.GetType().ToString() + " | "
					+ ((bd.Planet != null && bd.Planet.Kingdom != null) ? bd.Planet.Kingdom.Race.RaceType.ToString() + " " + bd.Planet.PlanetName : ""));
		}
		else if (GUILayout.Button("Save Game", guiDef.button, GUILayout.Width(91), GUILayout.Height(22)))
		{
			Core.Save();

			optButtons = false;
		}
		else if (GUILayout.Button("Load Game", guiDef.button, GUILayout.Width(91), GUILayout.Height(22)))
		{
			if (!File.Exists(Core.IndexPath) || !File.Exists(Core.DataPath))
			{
				Debug.Log("No saved files found!");
				optButtons = false;
				return;
			}

			Core.Load();
			optButtons = false;

			//testing
			foreach (StarSystem ss in Core.Stars.Values)
			{
				Debug.Log("Star: " + ss.StarName + " |" + ss.StarID.ToString());
				foreach (Planet pl in ss.Planets)
					Debug.Log("Planet: " + pl.PlanetName + " |" + pl.PlanetID);
			}
		}
		else if (GUILayout.Button("Quit Game", guiAlt.button, GUILayout.Width(91), GUILayout.Height(20)))
		{

		}

		GUILayout.EndScrollView();
	}

	void WinLaunchingShips(int windowID)
	{
		winscrollPos7 = GUILayout.BeginScrollView(winscrollPos7, GUILayout.Width(300), GUILayout.Height(60));

		foreach (Kingdom kdm in Core.galaxyKingdoms)
		{
			string[] text = Core.solarOnShow.GetFleetTexts(kdm);

			if (text[1].Trim().Length > 0 && GUILayout.Button(text[1], guiAlt.button))
			{
			}

			if (text[2].Trim().Length > 0 && GUILayout.Button(text[2], guiAlt.button))
			{

			}
		}

		GUILayout.EndScrollView();
		GUI.DragWindow();
	}

	void WinDockingShips(int windowID)
	{
		winscrollPos8 = GUILayout.BeginScrollView(winscrollPos8, GUILayout.Width(300), GUILayout.Height(60));

		foreach (Kingdom kdm in Core.galaxyKingdoms)
		{
			string[] text = Core.solarOnShow.GetFleetTexts(kdm);

			if (text[0].Trim().Length > 0 && GUILayout.Button(text[0], guiAlt.button))
			{

			}
		}

		GUILayout.EndScrollView();
		GUI.DragWindow();
	}

	void WinConfirmFleet(int windowID)
	{
		GUILayout.BeginVertical(guiSkin.box);

		GUILayout.Label("Send Ships to: " + Core.solarOnShow.StarName, guiAlt.box);

		if (GUILayout.Button("OK", guiAlt.button))
		{
			toConfirm = false;
			toStar = true;
			//isTop = true;
		}

		if (GUILayout.Button("Cancel", guiAlt.button))
		{
			toConfirm = false;
		}

		GUILayout.EndVertical();
		GUI.DragWindow();
	}

	void WinToColony(int windowID)
	{
		GUILayout.BeginVertical(guiSkin.box);

		GUILayout.Label("Settle Colony Ship: " + Core.solarOnShow.StarName, guiAlt.box);

		if (GUILayout.Button("OK", guiAlt.button))
		{
			toColony = false;

			if (Kingdom.IsOnePlanetLeft(Core.solarOnShow))
			{
				Planet plt = new Planet();
				int cnt = 0;

				foreach (Planet pl in Core.solarOnShow.Planets)
					if (!pl.Giant && pl.Status == Planet.PlanetStatus.None)
					{
						plt = pl;
						cnt++;
					}

				if (cnt > 0)
					kd.SettleColonyShip(Core.solarOnShow, plt);
			}
			else
			{
				strCommand = "Please select a planet to dock your Colony Ship!";
				isTop = true;
			}

		}

		if (GUILayout.Button("Cancel", guiAlt.button))
			toColony = false;

		GUILayout.EndVertical();
		GUI.DragWindow();
	}

	void WinShipList(int windowID)
	{
		winscrollPos6 = GUILayout.BeginScrollView(winscrollPos6, GUILayout.Width(300), GUILayout.Height(460));
		GUILayout.BeginVertical(guiSkin.box);

		if (GUILayout.Button("Cancel Sending Ships", guiSkin.button))
		{
			toStar = false;
			//isTop = false;
		}

		foreach (Fleet.SolarFleet sf in kd.KingdomFleets)
		{
			List<Fleet> lfls;

			if (sf.pDocks[0].Count > 0 || sf.pDocks[1].Count > 0 || sf.pDocks[2].Count > 0)
			{
				string[] txts = sf.pSolar.GetFleetTexts(kd);
				string sstart = sf.pSolar.StarName + " ";

				if (sf.pTurn > 0)
					sstart += toStar ? sf.pTurn.ToString() + " Turn " : ((int)(sf.pDistance / 1000)).ToString() + " Lightyear ";

				if (sf.pDocks[0].Count > 0 && GUILayout.Button("Dock:" + sstart + txts[0], guiAlt.button))
				{
					lfls = new List<Fleet>();

					foreach (Fleet.SelectFleet sl in sf.pDocks[0])
					{
						if (sl.pSelected)
							lfls.Add(sl.pShip);
					}

					if (lfls.Count > 0)
						kd.PrepareLaunching(lfls, Core.solarOnShow);

					toStar = false;			// to reset ship list display flag
					//isTop = false;
				}

				if (sf.pDocks[1].Count > 0 && GUILayout.Button("Launch:" + sstart + txts[1], guiAlt.button))
				{

				}

				if (sf.pDocks[2].Count > 0 && GUILayout.Button("Away:" + sstart + txts[2], guiAlt.button))
				{

				}
			}
		}



		GUILayout.EndVertical();
		GUILayout.EndScrollView();

		GUI.DragWindow();
	}

	void WinFacilityList(int windowID)
	{
		winscrollPos5 = GUILayout.BeginScrollView(winscrollPos5, GUILayout.Width(300), GUILayout.Height(460));
		GUILayout.BeginVertical(guiSkin.box);

		string text = "";

		if (Core.galaxyKingdoms[0].Home.Planets.Count > 0)
			foreach (Planet.PlanetFacility pf in Core.galaxyKingdoms[0].Home.Planets[0].Facilities)
			{
				text += "\n" + pf.pBuild.DispName;
			}

		if (text.Length > 0)
		{
			text = text.Substring(1);
			GUILayout.Label("Kingdom Facility");
			GUILayout.TextArea(text);
		}

		GUILayout.EndVertical();
		GUILayout.EndScrollView();

		GUI.DragWindow();
	}

	void WinInfolList(int windowID)
	{
		winscrollPos4 = GUILayout.BeginScrollView(winscrollPos4, GUILayout.Width(300), GUILayout.Height(460));
		GUILayout.BeginVertical(guiSkin.box);

		bool temp;

		if (GUILayout.Button("Owned Technology", guiDef.button))
		{
			temp = !infoKey[1];

			TurnOffSubInfo();
			infoKey[1] = temp;
		}

		if (GUILayout.Button("Owned Facility", guiDef.button))
		{
			temp = !infoKey[2];

			TurnOffSubInfo();
			infoKey[2] = temp;
		}

		if (GUILayout.Button("Kingdom Fleets", guiDef.button))
		{
			temp = !infoKey[3];

			TurnOffSubInfo();
			infoKey[3] = temp;

			if (temp && kd != null)
				kd.GatherKingdomFleets(Core.solarOnShow, false);
		}

		GUILayout.EndVertical();
		GUILayout.EndScrollView();
	}

	void WinTopShow(int windowID)
	{
		winscrollPos2 = GUILayout.BeginScrollView(winscrollPos2, GUILayout.Width(220), GUILayout.Height(65));

		GUILayout.TextArea(strCommand, guiAlt.textArea);

		GUILayout.EndScrollView();
		GUI.DragWindow();
	}

	void WinShowSummary(int windowID)
	{
		winscrollPos3 = GUILayout.BeginScrollView(winscrollPos3, GUILayout.Width(220), GUILayout.Height(250));

		if (GUILayout.Button("Close", guiAlt.button))
			kd.pOwnedTechString = kd.pUnlockedTechString = "";

		if (kd.pOwnedTechString.Length > 0)
		{
			GUILayout.Label("Technology developed", guiAlt.label);
			GUILayout.TextArea(kd.pOwnedTechString);
		}

		if (kd.pUnlockedTechString.Length > 0)
		{
			GUILayout.Label("New Technology found", guiAlt.label);
			GUILayout.TextArea(kd.pUnlockedTechString);
		}

		if (kd.pDevTechString.Length > 0)
		{
			GUILayout.Label("In Development", guiAlt.label);
			GUILayout.TextArea(kd.pDevTechString);
		}

		GUILayout.EndScrollView();
		GUI.DragWindow();
	}

	void WinTechList(int windowID)
	{
		winscrollPos1 = GUILayout.BeginScrollView(winscrollPos1, GUILayout.Width(300), GUILayout.Height(460));
		GUILayout.BeginVertical(guiSkin.box);

		int count = 0;

		foreach (Kingdom.KingdomTech ktech in kd.Tech)
		{
			GUILayout.Label(ktech.pCategory.ToString());

			string str = "";

			foreach (Kingdom.TechByCost tby in ktech.plTech)
			{
				if (tby.pStatus == Technology.TechStatus.Owned)			// display owned technology only
				{
					str += "\n" + tby.pName.ToString();
					count++;
				}
			}

			if (str.Length > 1)
				GUILayout.TextArea(str.Substring(1));
		}

		GUILayout.EndVertical();
		GUILayout.EndScrollView();

		GUI.DragWindow();
	}

	void WinTechContent(int windowID)
	{
		if (GUILayout.Button("Close", guiDef.button))
			techButton = false;

		winscrollPos = GUILayout.BeginScrollView(winscrollPos, GUILayout.Width(360), GUILayout.Height(540));
		GUILayout.BeginVertical(guiSkin.box);

		for (int i = 0; i < kd.Tech.Count; i++)
		{
			GUILayout.Label(kd.Tech[i].pCategory.ToString());

			if (stech[i].Length > 0)
			{
				if (GUILayout.Button("Next: " + stech[i], guiAlt.button))
				{
					for (int l = 0; l < nTech.Count; l++)
					{
						if (nTech[l].pName == stech[i])
							nTech.RemoveAt(l--);
					}

					stech[i] = "";
				}
			}

			for (int j = 0; j < kd.Tech[i].plTech.Count; j++)
			{
				if (kd.Tech[i].plTech[j].pStatus == Technology.TechStatus.Known)
				{
					if (GUILayout.Button(Utility.SplitPadName(kd.Tech[i].plTech[j].pName, kd.Tech[i].plTech[j].pCost, 40), guiSkin.textField))
					{
						for (int k = 0; k < nTech.Count; k++)
						{
							if (nTech[k].pName == stech[i])
								nTech.RemoveAt(k--);
						}

						if (nTech.Count + kd.Dev.Count < Kingdom.MaxTechDev && !DevContainsTech(kd.Tech[i].plTech[j]))					// allow only 3 technologies to be developed at the same time
						{
							stech[i] = kd.Tech[i].plTech[j].pName;
							nTech.Add(kd.Tech[i].plTech[j]);
						}
					}
				}

			}
		}


		GUILayout.EndVertical();
		GUILayout.EndScrollView();

		// GUI.DragWindow();
	}

	// class methods
	public bool DetectSendFleet()
	{
		if (isTop || kd.Fleets.Count == 0)
			return false;

		kd.GatherKingdomFleets(Core.solarOnShow, false);
		toConfirm = true;

		return true;
	}

	public bool DevContainsTech(Kingdom.TechByCost tech)
	{
		bool found = false;

		foreach (Kingdom.TechByCost tbc in kd.Dev)
			if (tbc.pTech.Name == tech.pTech.Name)
				found = true;

		return found;
	}

	public void TurnOffSubInfo()
	{
		// turn off other lists except infoKey[0]
		for (int i = 1; i < infoKey.Length; i++)
			infoKey[i] = false;
	}
}
